import { TargetObjectFilter, TargetObjectType, TargetObjectPlatformType, TargetObject } from './targetObject.js';
import { Engine } from '../engine.js';
import { Util } from '../../utils/util.js';

// customer.js
export const CustomerFilterState = {
    All: 'All',
    Enable: 'Enable',
    Disable: 'Disable'
}

export class CustomerFilter extends TargetObjectFilter {
    constructor() {
        super();
    }

    static clone(customerFilter) {
        let baseFilter = TargetObjectFilter.clone(customerFilter);

        let instance = new CustomerFilter();
        Object.assign(instance, baseFilter);
        return instance;
    }

    toJson() {
        return super.toJson();
    }
}

export class Customer extends TargetObject {
    constructor({
        id = '',
        name = '',
        contactName = '',
        contactPhoneNumber = '',
        email = '',
        city = '',
        address = '',
        remark = '',
        enabled = true,
    } = {}) {


        super({
            id: id,
            name: name,
            contactName: contactName,
            contactPhoneNumber: contactPhoneNumber,
            email: email,
            city: city,
            address: address,
            remark: remark,
            objType: TargetObjectType.TCustomer,
            enabled: enabled,
        });
    }

    static clone(customer) {
        const baseObj = TargetObject.clone(customer);

        // 创建Customer实例并拷贝属性
        const instance = new Customer();
        Object.assign(instance, baseObj);

        return instance;
    }

    static cloneFromTargetObject(targetObj) {

        const baseObj = TargetObject.clone(targetObj);

        // 创建Customer实例并拷贝属性
        const instance = new Customer();
        Object.assign(instance, baseObj);

        return instance;
    }

    static empty() {
        return new Customer();
    }

    static fromJson(json) {
        // 先调用父类方法创建基础对象
        const baseObj = TargetObject.fromJson(json, TargetObjectType.TCustomer);

        // 创建Customer实例并拷贝属性
        const instance = new Customer();
        Object.assign(instance, baseObj);
        return instance;
    }

    toJson() {
        return {
            id: this.id,
            code: this.code || '',
            orgId: Engine.profile.org.orgId,
            name: this.name,
            contactUser: this.contactName,
            contactPhone: this.contactPhoneNumber,
            contactEmail: this.email,
            city: this.city,
            detailAddr: this.address,
            remark: this.remark,
            customerType: 0,   // 0 客户， 1 供应商
            custState: this.enabled ? 1:0,
            ...(Util.isEmpty(this.id) && { ign_init_balance: this.initFundValue?.value || 0 })
        };
    }

    toCopyJson() {
        return super.toCopyJson();
    }
}